﻿#ifndef FLUTTEREVENTHANDLER
#define FLUTTEREVENTHANDLER
#include "event_stream_handler.h"
#include "flutter_blue_device_agent.h"
#include <windows.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Devices.Radios.h>
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.Advertisement.h>
#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.UI.Core.h>

#include <flutter/encodable_value.h>
#include <flutter/method_channel.h>
#include <flutter/basic_message_channel.h>
#include <flutter/event_channel.h>
#include <flutter/event_stream_handler_functions.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <flutter/standard_message_codec.h>

#include <map>
#include <memory>
#include <sstream>
#include <algorithm>
#include <iomanip>
using namespace winrt;
using namespace winrt::Windows::UI::Core;
using namespace winrt::Windows::Devices::Bluetooth;
using namespace winrt::Windows::Devices::Bluetooth::Advertisement;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Foundation::Collections;
using namespace winrt::Windows::Storage::Streams;
using namespace winrt::Windows::Devices::Radios;
using namespace winrt::Windows::Devices::Bluetooth;
using namespace winrt::Windows::Devices::Bluetooth::Advertisement;
using namespace winrt::Windows::Devices::Bluetooth::GenericAttributeProfile;
using namespace winrt::Windows::Devices::Enumeration;
using flutter::EncodableValue;
using flutter::EncodableMap;
class FlutterEventHandler {
public:
	// 定义回调函数类型
	using SendMessageConnectorCall = std::function<void(const flutter::EncodableValue&)>;
	using LogToFlutterCall = std::function<void(const std::string message)>;

	// 构造函数声明
	FlutterEventHandler(SendMessageConnectorCall messageConnectorCall, LogToFlutterCall logToFlutterCall);

	std::unique_ptr <EventStreamHandler<>> event_scan_handler;

	// 开始扫描
	void StartScan();

	// 停止扫描
	void StopScan();

	//蓝牙连接
	winrt::fire_and_forget ConnectAsync(uint64_t bluetoothAddress);
	//断开连接
	void CleanConnection(uint64_t bluetoothAddress, bool dispose);
	//这个设备是否连接
	boolean HaveConnectedDevices(uint64_t deviceId);
	BluetoothDeviceAgent* GetConnectedDevices(uint64_t deviceId);
	//发现服务内部异步
	winrt::fire_and_forget DiscoverServicesAsync(BluetoothDeviceAgent* bluetoothDeviceAgent);
	//设置通知
	winrt::fire_and_forget SetNotifiableAsync(BluetoothDeviceAgent* bluetoothDeviceAgent, std::string service, std::string characteristic, std::string bleInputProperty);
	//设置Mtu大小
	winrt::fire_and_forget RequestMtuAsync(BluetoothDeviceAgent* bluetoothDeviceAgent, uint64_t expectedMtu);
	//读取特征值
	winrt::fire_and_forget ReadValueAsync(BluetoothDeviceAgent* bluetoothDeviceAgent, std::string service, std::string characteristic);
	//写入特征值
	winrt::fire_and_forget WriteValueAsync(BluetoothDeviceAgent* bluetoothDeviceAgent, std::string service, std::string characteristic, std::vector <uint8_t> value, std::string bleOutputProperty);


private:
	// 存储回调函数
	SendMessageConnectorCall messageConnectorCall_;
	LogToFlutterCall logToFlutterCall_;
	//已经连接的对象key-value
	std::map <uint64_t, std::unique_ptr<BluetoothDeviceAgent>> connectedDevices{};


	// 扫描回调
	void BluetoothLEWatcher_Received(
		BluetoothLEAdvertisementWatcher sender,
		BluetoothLEAdvertisementReceivedEventArgs args);


	// 蓝牙扫描器
	BluetoothLEAdvertisementWatcher bluetoothLEWatcher{ nullptr };
	winrt::event_token bluetoothLEWatcherReceivedToken;

	void BluetoothLEDevice_ConnectionStatusChanged(BluetoothLEDevice sender, IInspectable args);

	void GattCharacteristic_Received(BluetoothDeviceAgent* bluetoothDeviceAgent, GattCharacteristic sender, GattValueChangedEventArgs args, std::string service,
		std::string characteristic);
};

#endif // FLUTTEREVENTHANDLER